<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------

namespace Report\Controller;
use User\Api\ReportApi;

class SystemController extends \Think\Controller {
    public function login($username = null, $password = null, $verify = null){
        if(IS_POST){
            $User = new ReportApi;
            $uid = $User->login($username, $password);
            if($uid > 0){ //UC登录成功
                session('report_id',$uid);
                $this->success('登录成功！', U('Report/newreport'));
            } else { //登录失败
                switch($uid) {
                    case -1: $error = '用户不存在或被禁用！'; break; //系统级别禁用
                    case -2: $error = '密码错误！'; break;
                    default: $error = '未知错误！'; break; // 0-接口参数错误（调试阶段使用）
                }
                $this->error($error);
            }
        } else {
            if(session('report_id')){
                $this->redirect('Report/newreport');
            }else{
                $this->username="GR";
                $this->password="123456";
                $this->title="登录";
                $this->display();
            }
        }
    }

    /* 退出登录 */
    public function logout(){
        if(session('report_id')){
            session('[destroy]');
            $this->success('退出成功！', U('login'));
        } else {
            $this->redirect('login');
        }
    }
    public function changepassword(){
        if(IS_POST){
            $map['account'] = I('account');
            $map['pwd'] = I('oldpassword');
            $data['pwd'] = I('newpassword');
            $res = M('report_user')->where($map)->save($data);
            if($res){
                $this->success("修改密码成功",U('login'));
            }else{
                $this->error("修改密码失败",U('changepassword'));
            }
        }else{
            $this->title = "修改密码";
            $this->display();
        }

    }
    public function register(){
        if(IS_POST){
            $M = M('report_user');
            $M->create();
            $M->createdAt = time();
            $M->updatedAt = time();
            $res = $M->add();
            if($res){
                $this->success("注册成功",U('login'));
            }else{
                $this->error("该工号已被注册",U('register'));
            }
        }else{
            $this->title = "注册新用户";
            $this->display();
        }
    }
    public function verify(){
        $verify = new \Think\Verify();
        $verify->entry(1);
    }


}
